home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1536 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  62 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Fastest way to index thru an array????
  5. Date: 11 Jan 1996 15:35:08 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan11103508@g7240065.bridge.bst.bls.com>
  8. References: <4d1g3k$o09@solaris.cc.vt.edu> <4d34p7$kj9@tahko.lpr.carel.fi>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: aril@cmt.lpr.mail.carel.fi's message of Thu, 11 Jan 1996 13:47:01
  11.     GMT
  12.  
  13. In article <4d34p7$kj9@tahko.lpr.carel.fi> aril@cmt.lpr.mail.carel.fi (Ari Lukumies) writes:
  14.  
  15. : Ashutosh Gokhale <ashutosh> wrote:
  16. :> Suppose I have an array A[][][] which I declare as
  17. :>     double ***A and then assign memory to it using new operator.
  18. :> Now consider that A has dimensions A[50][60][70]. Then I can index thru ALL
  19. :> entries of A using
  20. :> for(i=1; i<50; i++)
  21. :>  {
  22. :>    for(j=1; j<60; j++)
  23. :>     {
  24. :>       for(k=1; k<70; k++)
  25. :>        {
  26. :>          A[i][j][k] = <some expression>;
  27. :>        }
  28. :>     }
  29. :>  }
  30. :> But the above way is surely the most time-INEFFICIENT way.
  31. :> Can someone suggest me the MOST time efficient way of indexing through A[][][]?
  32.  
  33. : Try this:
  34.  
  35. :       double    *p = A;
  36. :       for (i = 0; i < 50*60*70; i++)
  37. :           *p++ = <some expression>;
  38.  
  39. : BTW, array indexes in C/C++ begin at 0, _not_ at 1.
  40.  
  41. The original poster send A was of type double***
  42. If the allocation was done like:
  43.  
  44.   A = new double**[50];
  45.   for (int i = 0; i < 50; i++) {
  46.       A[i] = new double*[60];
  47.       for (int j = 0; j < 60; j++) {
  48.           A[i][j] = new double[70];
  49.       }
  50.   }
  51.  
  52. Then   
  53.   double* p = A;
  54. is not sufficient. And without a cast it wouldn't work.
  55.  
  56. Regards
  57.  
  58.    -A
  59.  
  60. -- 
  61. | A.Champion                |
  62.